package org.bleep.util;

import org.lwjgl.opengl.GL11;

public class Color
{
	public static Color RED = new Color(1.0f, 0.0f, 0.0f);
	public static Color GREEN = new Color(0.0f, 1.0f, 0.0f);
	public static Color BLUE = new Color(0.0f, 0.0f, 0.0f);
	
	private static final float MAXIMUM = 1f;
	private static final float MINIMUM = 0f;
	
	private float r;
	private float g;
	private float b;
	
	public Color(float[] colorArray)
	{
		this(colorArray[0], colorArray[1], colorArray[2]);
	}
	
	public Color(float r, float g, float b)
	{
		r = r >= MAXIMUM ? MAXIMUM : r;
		g = g >= MAXIMUM ? MAXIMUM : g;
		b = b >= MAXIMUM ? MAXIMUM : b;
		this.r = r <= MINIMUM ? MINIMUM : r;
		this.g = g <= MINIMUM ? MINIMUM : g;
		this.b = b <= MINIMUM ? MINIMUM : b;
	}
	
	public float getR()
	{
		return r;
	}
	
	public float getG()
	{
		return g;
	}
	
	public float getB()
	{
		return b;
	}
	
	public float[] toArray()
	{
		float[] result = new float[3];
		result[0] = r;
		result[1] = g;
		result[2] = b;
		return result;
	}
	
	public void glColor()
	{
		GL11.glColor3f(r, g, b);
	}
	
	public static Color add(Color c1, Color c2)
	{
		float r = c1.getR() + c2.getR() >= MAXIMUM ? MAXIMUM : c1.getR() + c2.getR();
		float g = c1.getG() + c2.getG() >= MAXIMUM ? MAXIMUM : c1.getG() + c2.getG();
		float b = c1.getB() + c2.getB() >= MAXIMUM ? MAXIMUM : c1.getB() + c2.getB();
		return new Color(r, g, b);
	}
	
	public static Color sub(Color c1, Color c2)
	{
		float r = c1.getR() - c2.getR() <= MINIMUM ? MINIMUM : c1.getR() - c2.getR();
		float g = c1.getG() - c2.getG() <= MINIMUM ? MINIMUM : c1.getG() - c2.getG();
		float b = c1.getB() - c2.getB() <= MINIMUM ? MINIMUM : c1.getB() - c2.getB();
		return new Color(r, g, b);
	}
	
	public static Color mult(Color c1, float n)
	{
		float r = c1.getR() * n >= MAXIMUM ? MAXIMUM : c1.getR() * n;
		float g = c1.getG() * n >= MAXIMUM ? MAXIMUM : c1.getG() * n;
		float b = c1.getB() * n >= MAXIMUM ? MAXIMUM : c1.getB() * n;
		return new Color(r, g, b);
	}
}